from django.db import models
from Login.models import User

# 聊天室模型
class ChatRoom(models.Model):
    ROOM_TYPE_CHOICES = [
        ('private', '私聊'),
        ('group', '群聊'),
    ]
    
    name = models.CharField(max_length=128, blank=True, null=True, verbose_name='聊天室名称')
    room_type = models.CharField(max_length=16, choices=ROOM_TYPE_CHOICES, default='private', verbose_name='聊天室类型')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    updated_at = models.DateTimeField(auto_now=True, verbose_name='更新时间')
    
    class Meta:
        verbose_name = '聊天室'
        verbose_name_plural = '聊天室'
    
    def __str__(self):
        if self.room_type == 'private':
            return f"私聊房间 {self.id}"
        return f"群聊: {self.name}"

# 聊天室成员模型
class ChatRoomMember(models.Model):
    room = models.ForeignKey(ChatRoom, on_delete=models.CASCADE, related_name='members', verbose_name='聊天室')
    user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='chat_rooms', verbose_name='用户')
    joined_at = models.DateTimeField(auto_now_add=True, verbose_name='加入时间')
    is_admin = models.BooleanField(default=False, verbose_name='是否为管理员')
    
    class Meta:
        unique_together = ('room', 'user')
        verbose_name = '聊天室成员'
        verbose_name_plural = '聊天室成员'
    
    def __str__(self):
        return f"{self.user.username} in {self.room}"

# 聊天消息模型
class ChatMessage(models.Model):
    MESSAGE_TYPE_CHOICES = [
        ('text', '文本'),
        ('image', '图片'),
        ('file', '文件'),
        ('system', '系统消息'),
    ]
    
    room = models.ForeignKey(ChatRoom, on_delete=models.CASCADE, related_name='messages', verbose_name='聊天室')
    sender = models.ForeignKey(User, on_delete=models.CASCADE, related_name='sent_messages', verbose_name='发送者')
    message_type = models.CharField(max_length=16, choices=MESSAGE_TYPE_CHOICES, default='text', verbose_name='消息类型')
    content = models.TextField(verbose_name='消息内容')
    file_url = models.URLField(blank=True, null=True, verbose_name='文件URL')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='发送时间')
    is_read = models.BooleanField(default=False, verbose_name='是否已读')
    
    class Meta:
        ordering = ['created_at']
        verbose_name = '聊天消息'
        verbose_name_plural = '聊天消息'
    
    def __str__(self):
        return f"{self.sender.username}: {self.content[:50]}"

# 消息已读状态模型
class MessageReadStatus(models.Model):
    message = models.ForeignKey(ChatMessage, on_delete=models.CASCADE, related_name='read_status', verbose_name='消息')
    user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='read_messages', verbose_name='用户')
    read_at = models.DateTimeField(auto_now_add=True, verbose_name='阅读时间')
    
    class Meta:
        unique_together = ('message', 'user')
        verbose_name = '消息已读状态'
        verbose_name_plural = '消息已读状态'
    
    def __str__(self):
        return f"{self.user.username} read message {self.message.id}"
